package com.turtle3d.visualizator;

import java.util.ArrayList;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Material;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Matrix4f;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.Cylinder;
import com.turtle3d.formallanguage.VisualSymbol;
import com.turtle3d.formallanguage.symbol.Symbol;


public class Turtle3DVisualisator {

	/**
	 * @param args
	 */
	
	public static BranchGroup createBranchGroup(VisualData visualData){
		BranchGroup branchGroup = new BranchGroup();
		
		//branchGroup.addChild(Line3D.test().toShape3D());
		ArrayList<VisualSymbol> visualSequence = visualData.getVisualSequence();
				
		for(int i=1;i<visualSequence.size();i++){
			
			if(!visualSequence.get(i-1).getSymbol().equals(new Symbol("F")))
				continue;
			int depth = visualSequence.get(i-1).getStep();
			
			Point3f pos1 = new Point3f();
			Point3f pos2 = new Point3f();
			visualSequence.get(i-1).getTransform().transform(pos1);
			visualSequence.get(i).getTransform().transform(pos2);
			
			Matrix4f transform = new Matrix4f();
			transform.setIdentity();
			
			Float d = visualSequence.get(i-1).getSymbol().getParameter("distance");
			float normalizedDepth = depth/(visualData.getMaximalDepthReached()*1f);
			
			Matrix4f orginFix = new Matrix4f();
			orginFix.setIdentity();
			orginFix.setTranslation(new Vector3f(0,d/2f,0));			
						
			transform.mul(visualSequence.get(i-1).getTransform());
			//transform.mul(orginFix);
			
			orginFix.invert();
			//temp.mul(m);
			
			//System.out.println(history.get(i-1));						
			//System.out.println("Adding "+pos1+" "+pos2);
			
			//branchGroup.addChild((new Line3D(pos1, pos2, new Color3f(Color.WHITE))).toShape3D() );
			
			TransformGroup tg = new TransformGroup();
			Transform3D t = new Transform3D();			
			
			t.set(transform);			
			
			//tg.setTransform(t);	
			Float r = visualSequence.get(i-1).getSymbol().getParameter("radius");			
			
			MyCone c = new MyCone(d,r,r*0.7f, d>0.5f?8:3);			
			//Box c = new Box(0.02f, 0.1f, 0.02f, null);
			//ColoringAttributes coloringAttributes = new ColoringAttributes(new Color3f(1f,0f,0f), ColoringAttributes.SHADE_GOURAUD);
			Appearance appearance = new Appearance();
			//appearance.setColoringAttributes(coloringAttributes);
			Material m = new Material();
			//m.setAmbientColor(new Color3f(1f,0f,0f));
			
			
			m.setDiffuseColor(new Color3f(1f-normalizedDepth,(float)Math.sqrt(normalizedDepth),0f));
			appearance.setMaterial(m);
			c.setAppearance(appearance);
			
			tg.addChild(c);
			tg.setTransform(t);
			//c.set
			//System.out.println(i);
			branchGroup.addChild(tg);
			
		}
		
		
		//TransformGroup tg = ObjectCreator.createCylinder(0.02f, 0.2f);
		/*TransformGroup tg = new TransformGroup();
		
		Transform3D rot = new Transform3D();
		//t.setTranslation(new Vector3f(0f,-1f,0f));
		
		Matrix4f m = new Matrix4f();
		m.setIdentity();
		m.setTranslation(new Vector3f(0,-0.2f,0));
		
		Matrix4f r = new Matrix4f();
		r.setIdentity();
		r.rotZ((float)(Math.PI/4f));
		
		Matrix4f temp = new Matrix4f(m);
		temp.mul(r);
		
		m.invert();
		temp.mul(m);
						
								
		rot.set(temp);
		//t.setTranslation(new Vector3f(0,-0.2f,0));
		
		tg.setTransform(rot);	
		
		
		//Cylinder c = new Cylinder(0.02f, 0.2f);			
		Box c = new Box(0.02f, 0.2f, 0.02f, null);
		tg.addChild(c);
		
		branchGroup.addChild(tg);
		branchGroup.addChild(new Box(0.02f, 0.2f, 0.02f, null));*/
		
		return branchGroup;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	}

}
